using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using GoogleEarthClasses.Global;
namespace GoogleEarthClasses.Data
{
    public class DPolygon
    {
        public static Int32 Insert(string CreatedBy, string CreatedAt, string UpdatedBy, string UpdatedAt, string UpdatedReason, string Name, string TypeName, string Points, string Description)
        {
            SqlCommand _Command = null;
            SqlTransaction _Transaction = null;
            try
            {
            _Command = new SqlCommand("sp_Polygons_Insert", DBHelper.GetConnection());
            _Transaction = _Command.Connection.BeginTransaction();
            _Command.Transaction = _Transaction;
            _Command.CommandType = CommandType.StoredProcedure;
            #region CreatedBy
            if (string.IsNullOrEmpty(CreatedBy))
                _Command.Parameters.Add(DBHelper.CreateTextParameter("@CreatedBy", ParameterDirection.Input, false, DBNull.Value));
            else
                _Command.Parameters.Add(DBHelper.CreateTextParameter("@CreatedBy", ParameterDirection.Input, false, CreatedBy));
            #endregion CreatedBy
            #region CreatedAt
            if (string.IsNullOrEmpty(CreatedAt))
                _Command.Parameters.Add(DBHelper.CreateTextParameter("@CreatedAt", ParameterDirection.Input, false, DBNull.Value));
            else
                _Command.Parameters.Add(DBHelper.CreateTextParameter("@CreatedAt", ParameterDirection.Input, false, CreatedAt));
            #endregion CreatedAt
            #region UpdatedBy
            if (string.IsNullOrEmpty(UpdatedBy))
                _Command.Parameters.Add(DBHelper.CreateTextParameter("@UpdatedBy", ParameterDirection.Input, false, DBNull.Value));
            else
                _Command.Parameters.Add(DBHelper.CreateTextParameter("@UpdatedBy", ParameterDirection.Input, false, UpdatedBy));
            #endregion UpdatedBy
            #region UpdatedAt
            if (string.IsNullOrEmpty(UpdatedAt))
                _Command.Parameters.Add(DBHelper.CreateTextParameter("@UpdatedAt", ParameterDirection.Input, false, DBNull.Value));
            else
                _Command.Parameters.Add(DBHelper.CreateTextParameter("@UpdatedAt", ParameterDirection.Input, false, UpdatedAt));
            #endregion UpdatedAt
            #region UpdatedReason
            if (string.IsNullOrEmpty(UpdatedReason))
                _Command.Parameters.Add(DBHelper.CreateTextParameter("@UpdatedReason", ParameterDirection.Input, false, DBNull.Value));
            else
                _Command.Parameters.Add(DBHelper.CreateTextParameter("@UpdatedReason", ParameterDirection.Input, false, UpdatedReason));
            #endregion UpdatedReason
            #region Name
            if (string.IsNullOrEmpty(Name))
                _Command.Parameters.Add(DBHelper.CreateTextParameter("@Name", ParameterDirection.Input, false, DBNull.Value));
            else
                _Command.Parameters.Add(DBHelper.CreateTextParameter("@Name", ParameterDirection.Input, false, Name));
            #endregion Name
            #region TypeName
            if (string.IsNullOrEmpty(TypeName))
                _Command.Parameters.Add(DBHelper.CreateTextParameter("@TypeName", ParameterDirection.Input, false, DBNull.Value));
            else
                _Command.Parameters.Add(DBHelper.CreateTextParameter("@TypeName", ParameterDirection.Input, false, TypeName));
            #endregion TypeName
            #region Points
            if (string.IsNullOrEmpty(Points))
                _Command.Parameters.Add(DBHelper.CreateTextParameter("@Points", ParameterDirection.Input, false, DBNull.Value));
            else
                _Command.Parameters.Add(DBHelper.CreateTextParameter("@Points", ParameterDirection.Input, false, Points));
            #endregion Points
            #region Description
            if (string.IsNullOrEmpty(Description))
                _Command.Parameters.Add(DBHelper.CreateTextParameter("@Description", ParameterDirection.Input, false, DBNull.Value));
            else
                _Command.Parameters.Add(DBHelper.CreateTextParameter("@Description", ParameterDirection.Input, false, Description));
            #endregion Description

            _Command.Parameters.Add(DBHelper.CreateIntParameter("@PolygonId", ParameterDirection.ReturnValue, false, 0));
            _Command.ExecuteNonQuery();
            _Transaction.Commit();
            return (Int32)_Command.Parameters["@PolygonId"].Value;

            }
            catch (Exception ex)
            {
                if (_Transaction != null)
                    _Transaction.Rollback();
                throw ex;
            }
            finally
            {
                if (_Command != null) _Command.Connection.Close();
            }
         }
        public static void Update(Int32 PolygonId, string UpdatedBy, string UpdatedAt, string UpdatedReason, string Name, string TypeName, string Points, string Description)
        {
            SqlCommand _Command = null;
            SqlTransaction _Transaction = null;
            try
            {
                _Command = new SqlCommand("sp_Polygons_Update", DBHelper.GetConnection());
                _Transaction = _Command.Connection.BeginTransaction();
                _Command.Transaction = _Transaction;
                _Command.CommandType = CommandType.StoredProcedure;

                _Command.Parameters.Add(DBHelper.CreateIntParameter("@PolygonId", ParameterDirection.Input, false, PolygonId));

                #region UpdatedBy
                if (string.IsNullOrEmpty(UpdatedBy))
                    _Command.Parameters.Add(DBHelper.CreateTextParameter("@UpdatedBy", ParameterDirection.Input, false, DBNull.Value));
                else
                    _Command.Parameters.Add(DBHelper.CreateTextParameter("@UpdatedBy", ParameterDirection.Input, false, UpdatedBy));
                #endregion UpdatedBy
                #region UpdatedAt
                if (string.IsNullOrEmpty(UpdatedAt))
                    _Command.Parameters.Add(DBHelper.CreateTextParameter("@UpdatedAt", ParameterDirection.Input, false, DBNull.Value));
                else
                    _Command.Parameters.Add(DBHelper.CreateTextParameter("@UpdatedAt", ParameterDirection.Input, false, UpdatedAt));
                #endregion UpdatedAt
                #region UpdatedReason
                if (string.IsNullOrEmpty(UpdatedReason))
                    _Command.Parameters.Add(DBHelper.CreateTextParameter("@UpdatedReason", ParameterDirection.Input, false, DBNull.Value));
                else
                    _Command.Parameters.Add(DBHelper.CreateTextParameter("@UpdatedReason", ParameterDirection.Input, false, UpdatedReason));
                #endregion UpdatedReason
                #region Name
                if (string.IsNullOrEmpty(Name))
                    _Command.Parameters.Add(DBHelper.CreateTextParameter("@Name", ParameterDirection.Input, false, DBNull.Value));
                else
                    _Command.Parameters.Add(DBHelper.CreateTextParameter("@Name", ParameterDirection.Input, false, Name));
                #endregion Name
                #region TypeName
                if (string.IsNullOrEmpty(TypeName))
                    _Command.Parameters.Add(DBHelper.CreateTextParameter("@TypeName", ParameterDirection.Input, false, DBNull.Value));
                else
                    _Command.Parameters.Add(DBHelper.CreateTextParameter("@TypeName", ParameterDirection.Input, false, TypeName));
                #endregion TypeName
                #region Points
                if (string.IsNullOrEmpty(Points))
                    _Command.Parameters.Add(DBHelper.CreateTextParameter("@Points", ParameterDirection.Input, false, DBNull.Value));
                else
                    _Command.Parameters.Add(DBHelper.CreateTextParameter("@Points", ParameterDirection.Input, false, Points));
                #endregion Points
                #region Description
                if (string.IsNullOrEmpty(Description))
                    _Command.Parameters.Add(DBHelper.CreateTextParameter("@Description", ParameterDirection.Input, false, DBNull.Value));
                else
                    _Command.Parameters.Add(DBHelper.CreateTextParameter("@Description", ParameterDirection.Input, false, Description));
                #endregion Description

                _Command.ExecuteNonQuery();
                _Transaction.Commit();
            }
            catch (Exception ex)
            {
                if (_Transaction != null)
                    _Transaction.Rollback();
                throw ex;
            }
            finally
            {
                if (_Command != null) _Command.Connection.Close();
            }
        }
        public static void Delete(int PolygonId)
        { 
            SqlCommand _Command = null;
            SqlTransaction _Transaction = null;
            try
            {
                _Command = new SqlCommand("sp_Polygons_Delete", DBHelper.GetConnection());
                _Transaction = _Command.Connection.BeginTransaction();
                _Command.Transaction = _Transaction;

                _Command.CommandType = CommandType.StoredProcedure;
                _Command.Parameters.Add(DBHelper.CreateIntParameter("@PolygonId", ParameterDirection.Input, false, PolygonId));
                _Command.ExecuteNonQuery();
                _Transaction.Commit();
            }
            catch (Exception ex)
            {
                if (_Transaction != null)
                    _Transaction.Rollback();
                throw ex;
            }
            finally
            {
                if (_Command != null) _Command.Connection.Close();
            }
        }

    }
}
